<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="咨询人信息" prop="searchValue">
        <el-input
            v-model="queryParams.searchValue"
            placeholder="姓名/手机号"
            clearable
            @keyup.enter="handleQuery"
            style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="能力等级" prop="abilityLevel">
        <el-select v-model="queryParams.abilityLevel" placeholder="请选择能力等级" clearable style="width: 240px;">
          <el-option
              v-for="dict in ability_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户等级" prop="customerLevel">
        <el-input
            v-model="queryParams.customerLevel"
            placeholder="请输入客户等级"
            clearable
            @keyup.enter="handleQuery"
            style="width: 240px;"
        />
      </el-form-item>
      <el-form-item label="咨询状态" prop="consultationStatus">
        <el-select v-model="queryParams.consultationStatus" placeholder="请选择客咨询状态" clearable
                   style="width: 240px;">
          <el-option label="咨询中" value="1"/>
          <el-option label="已入住" value="2"/>
          <el-option label="已关闭" value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="日期范围" style="width: 300px;">
        <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['pg:consult:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <!--   @selection-change="handleSelectionChange"
          <el-table-column type="selection" width="55" align="center"/> -->
    <el-table v-loading="loading" :data="consultList">
      <el-table-column label="序号" align="center" type="index" width="60"/>
      <el-table-column label="咨询人信息" align="center" prop="consultantName1">
        <template #default="scope">
          <div>{{ scope.row.consultantName1 }}//{{ scope.row.consultantPhone1 }}</div>
        </template>
      </el-table-column>
      <el-table-column label="长者姓名" align="center" prop="elderlyName">
        <template #default="scope">
          <div>{{ scope.row.elderlyName }}//{{ scope.row.elderlyPhone }}</div>
        </template>
      </el-table-column>
      <el-table-column label="与长者关系" align="center" prop="relationship1">
        <template #default="scope">
          <dict-tag :options="elderly_relationship" :value="scope.row.relationship1"/>
        </template>
      </el-table-column>
      <el-table-column label="咨询日期" align="center" prop="createTime" width="180"/>
      <el-table-column label="咨询状态" align="center" prop="consultationStatus">
        <template #default="scope">
          <div v-if="scope.row.consultationStatus==1">咨询中</div>
          <div v-if="scope.row.consultationStatus==2">已入住</div>
          <div v-if="scope.row.consultationStatus==3">已关闭</div>
        </template>
      </el-table-column>
      <el-table-column label="客户等级" align="center" prop="customerLevel"/>
      <el-table-column label="客户渠道" align="center" prop="customerChannel">
        <template #default="scope">
          <dict-tag :options="customer_channel" :value="scope.row.customerChannel"/>
        </template>
      </el-table-column>
      <el-table-column label="咨询原因" align="center" prop="consultationReason">
        <template #default="scope">
          <dict-tag :options="reason_for_consultation" :value="scope.row.consultationReason"/>
        </template>
      </el-table-column>
      <el-table-column label="咨询详情" align="center" prop="consultationDetails" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template #default="scope">
          <el-button
              type="text"
              icon="View"
              @click="handleDetails(scope.row)"
          >咨询详情
          </el-button>
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改咨询对话框 -->
    <el-dialog :title="title" v-model="open" width="1200" append-to-body>
      <el-form ref="consultRef" :model="form" :rules="rules" label-width="140px">
        <div class="title_">第一咨询人</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="姓名" prop="consultantName1">
              <el-input v-model="form.consultantName1" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号" prop="consultantPhone1">
              <el-input v-model="form.consultantPhone1" placeholder="请输入手机号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="与长者关系" prop="relationship1">
              <el-select v-model="form.relationship1" placeholder="请选择与长者关系">
                <el-option
                    v-for="dict in elderly_relationship"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <div class="title_">第二咨询人</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="姓名" prop="twoConsultantName">
              <el-input v-model="form.twoConsultantName" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号" prop="twoConsultantPhone">
              <el-input v-model="form.twoConsultantPhone" placeholder="请输入手机号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="人与长者关系" prop="twoConsultantRelation">
              <el-select v-model="form.twoConsultantRelation" placeholder="请选择与长者关系">
                <el-option
                    v-for="dict in elderly_relationship"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <div class="title_">长者信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="长者姓名" prop="elderlyName">
              <el-input v-model="form.elderlyName" placeholder="请输入长者姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="长者生日" prop="elderlyBirthday">
              <el-date-picker clearable
                              v-model="form.elderlyBirthday"
                              type="date"
                              value-format="YYYY-MM-DD"
                              style="width: 100%"
                              placeholder="请选择长者生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="长者手机号" prop="elderlyPhone">
              <el-input v-model="form.elderlyPhone" placeholder="请输入长者手机号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="长者性别">
              <el-radio-group v-model="form.elderlyGender">
                <el-radio
                    v-for="dict in sys_user_sex"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="长者身份证" prop="elderlyIdCard">
              <el-input v-model="form.elderlyIdCard" placeholder="请输入长者身份证"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="民族" prop="ethnicity">
              <el-select v-model="form.ethnicity" placeholder="请选择民族">
                <el-option
                    v-for="dict in nation_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="能力等级" prop="abilityLevel">
              <el-select v-model="form.abilityLevel" placeholder="请选择能力等级">
                <el-option
                    v-for="dict in ability_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否入住过养老机构" prop="livedIn">
              <el-radio-group v-model="form.livedIn">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.livedIn == '1'">
            <el-form-item label="养老机构名称" prop="nursingHome">
              <el-input v-model="form.nursingHome" placeholder="请输入养老机构名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户等级" prop="customerLevel">
              <el-input v-model="form.customerLevel" placeholder="请输入客户等级"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title_">咨询信息</div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="咨询意向" prop="consultationIntention">
              <el-select v-model="form.consultationIntention" placeholder="请选择咨询意向">
                <el-option
                    v-for="dict in consultation_intention"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户渠道" prop="customerChannel">
              <el-select v-model="form.customerChannel" placeholder="请选择客户渠道">
                <el-option
                    v-for="dict in customer_channel"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="咨询原因" prop="consultationReason">
              <el-select v-model="form.consultationReason" placeholder="请选择咨询原因">
                <el-option
                    v-for="dict in reason_for_consultation"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="介绍人" prop="introducer">
              <el-input v-model="form.introducer" placeholder="请输入介绍人"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="咨询详情" prop="consultationDetails">
          <el-input v-model="form.consultationDetails" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="附件">
          <image-upload v-model="form.attachment"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Consult">
import {addDateRange} from "@/utils/ruoyi.js";
import router from '@/router'
import {
  addConsultation,
  delConsultation,
  getConsultation,
  listConsultation, updateConsultation
} from "@/views/record/consultManage/api/consultation.js";

const {proxy} = getCurrentInstance();
const {
  elderly_relationship,
  ability_level,
  reason_for_consultation,
  sys_user_sex,
  consultation_intention,
  customer_channel,
  nation_type
} = proxy.useDict('elderly_relationship', 'ability_level', 'reason_for_consultation', 'sys_user_sex', 'consultation_intention', 'customer_channel', 'nation_type');

const consultList = ref([]);
const value1 = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    consultantName1: null,
    consultantPhone1: null,
    relationship1: null,
    twoConsultantName: null,
    twoConsultantPhone: null,
    twoConsultantRelation: null,
    elderlyName: null,
    elderlyBirthday: null,
    elderlyPhone: null,
    elderlyGender: null,
    elderlyIdCard: null,
    ethnicity: null,
    abilityLevel: null,
    livedIn: null,
    customerLevel: null,
    consultationIntention: null,
    customerChannel: null,
    consultationReason: null,
    introducer: null,
    consultationDetails: null,
    attachment: null,
    consultationStatus: null,
    remark: null,
  },
  rules: {
    consultantName1: [
      {required: true, message: "第一咨询人姓名不能为空", trigger: "blur"}
    ],
    consultantPhone1: [
      {required: true, message: "第一咨询人手机号不能为空", trigger: "blur"}
    ],
    relationship1: [
      {required: true, message: "第一咨询人与长者关系不能为空", trigger: "change"}
    ],
    elderlyName: [
      {required: true, message: "长者姓名不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);


getList();

/** 查询咨询列表 */
function getList() {
  loading.value = true;
  listConsultation(addDateRange(queryParams.value, value1.value)).then(response => {
    consultList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    consultantName1: null,
    consultantPhone1: null,
    relationship1: null,
    twoConsultantName: null,
    twoConsultantPhone: null,
    twoConsultantRelation: null,
    elderlyName: null,
    elderlyBirthday: null,
    elderlyPhone: null,
    elderlyGender: "0",
    elderlyIdCard: null,
    ethnicity: null,
    abilityLevel: null,
    livedIn: null,
    customerLevel: null,
    consultationIntention: null,
    customerChannel: null,
    consultationReason: null,
    introducer: null,
    consultationDetails: null,
    attachment: null,
    consultationStatus: "1",
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  proxy.resetForm("consultRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  value1.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加咨询";
}

/** 查看按钮操作 */
function handleDetails(row) {
  router.push({
    path: "/customer_management/consultInfo",
    query: {
      id: row.id,
    },
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getConsultation(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改咨询";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["consultRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateConsultation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addConsultation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除咨询编号为"' + _ids + '"的数据项？').then(function () {
    return delConsultation(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pg/consult/export', {
    ...queryParams.value
  }, `consult_${new Date().getTime()}.xlsx`)
}
</script>
<style scoped>
.title_ {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
  margin-left: 20px;
}
</style>